package com.hifar.test.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hifar.fw.utils.id.IdUtils;
import com.hifar.plat.officUtil.ExcelTemplateUtil;
import com.hifar.sys.BusiResponse;
import com.hifar.sys.RequestConver;
import com.hifar.test.product.mapper.HfProductBaseMapper;
import com.hifar.test.product.pojo.HfProductBase;
import com.hifar.test.product.service.IHfProductBaseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 产品-管理 服务实现类
 * </p>
 *
 * @author wangkepeng
 * @since 2024-12-05 14:47:08
 */
@Service
public class HfProductBaseServiceImpl extends ServiceImpl<HfProductBaseMapper, HfProductBase> implements IHfProductBaseService {


    @Override
    public BusiResponse exportExcel(Map<String, Object> pageInfo) {
        BusiResponse busiResponse = new BusiResponse();
        String ids = (String) pageInfo.get("ids");

        String operationType = (String) pageInfo.get("operationType");

        if (StringUtils.isNotBlank(operationType) && (operationType.equals("template"))) {
            ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfProductBase.class, "导入模板", "产品-管理导入数据");
            return busiResponse;
        } else {
            QueryWrapper<HfProductBase> postParam = RequestConver.getPostParam(pageInfo);
            postParam.ne("del_flag", 1);
            postParam.orderByDesc("create_time");
            if (StringUtils.isNotBlank(ids)) {
                postParam.in("id", ids);
            }
            List<HfProductBase> dataList = this.list(postParam);
            if (dataList != null && dataList.size() > 0) {
                ExcelTemplateUtil.exportExcel(busiResponse, dataList, HfProductBase.class, "产品-管理数据", "产品-管理导出");
            } else {
                ExcelTemplateUtil.exportExcelTemplate(busiResponse, HfProductBase.class, "产品-管理数据", "产品-管理导出");
            }
            return busiResponse;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public BusiResponse importExcel(MultipartFile file) throws Exception {
        if (file != null && !file.isEmpty()) {
            List<HfProductBase> importExcel = ExcelTemplateUtil.importExcel(file.getInputStream(), HfProductBase.class);
            List<HfProductBase> importExcelList = new ArrayList<>();
            if (importExcel != null && importExcel.size() > 0) {
                for (HfProductBase bean : importExcel) {
                    bean.setId(IdUtils.uuid32());
//					bean.setDelFlag(0);
                    //if(!bean.getName().equals("")) {
                    importExcelList.add(bean);
                    //}
                }
                this.saveBatch(importExcelList);
                return BusiResponse.ok("导入成功！");
            }
        }
        return BusiResponse.error("导入失败，原因未知！");
    }

    @Override
    public String saveOnExist(String productAlias, String productCode, String productName) {
        QueryWrapper<HfProductBase> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(HfProductBase::getProductAlias, productAlias)
                .eq(HfProductBase::getProductChartNo, productCode)
                .eq(HfProductBase::getProductName, productName);
        HfProductBase hfProductBase = this.getOne(queryWrapper);
        if (Objects.nonNull(hfProductBase)) {
            return hfProductBase.getId();
        }
        HfProductBase newHfProductBase = new HfProductBase();
        newHfProductBase.setProductAlias(productAlias);
        newHfProductBase.setProductName(productName);
        newHfProductBase.setProductCode(productCode);
//		newHfProductBase.setSecretLevel(1);
        newHfProductBase.setProductEffect(1);
        newHfProductBase.setStatus("1");
        this.save(newHfProductBase);
        return newHfProductBase.getId();
    }
}
